package com.myz.app.example.po;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户表
 * @author maoyz on 18-4-9.
 */
public class User implements Serializable {

    private Long id;
    /**
     * 所属公司
     */
    private Long organizationId;

    private String username;
    private String password;
    /**
     * 加密密码的盐
     */
    private String salt;
    /**
     * 拥有的角色列表
     */
    private List<Long> roleIds;
    private Boolean locked = Boolean.FALSE;

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(Long organizationId) {
        this.organizationId = organizationId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public List<Long> getRoleIds() {
        // 加入判断
        if (roleIds == null){
            roleIds = new ArrayList<Long>();
        }
        return roleIds;
    }

    public void setRoleIds(List<Long> roleIds) {
        this.roleIds = roleIds;
    }

    /**
     * 拼装RoleIds
     */
    public String getRolesStr(){
        if (CollectionUtils.isEmpty(roleIds)){
            return "";
        }
        StringBuilder builder = new StringBuilder();
        for (Long roleId : roleIds) {
            builder.append(roleId);
            builder.append(",");
        }
        return builder.toString();
    }

    public void setRolesStr(String roleIdsStr){
        if (StringUtils.isEmpty(roleIdsStr)){
            return;
        }
        String[] split = roleIdsStr.split(",");
        for (String roleId : split) {
            getRoleIds().add(Long.valueOf(roleId));
        }
    }


    public Boolean getLocked() {
        return locked;
    }

    public void setLocked(Boolean locked) {
        this.locked = locked;
    }

    /**
     * 获取盐值
     */
    public String getCredentialsSalt() {
        return username + salt;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", organizationId=" + organizationId +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", salt='" + salt + '\'' +
                ", roleIds=" + roleIds +
                ", locked=" + locked +
                '}';
    }
}
